import copy
import json
import os

#修改json文件中的label
# def extract(path, json_name, save_dir):
#     json_file = open(path, 'r', encoding='utf-8')
#     data = json.load(json_file)
#     shapes = data["shapes"]
#     labels = []
#     for i in range(len(shapes)):
#         if shapes[i]['label'] == 'liewen':
#             labels += [shapes[i]]
#     data['shapes'] = labels
#     print(json_name)
#     with open(save_dir + str(json_name), 'w') as f:
#         json.dump(data, f)
#         f.close()

# if __name__ == '__main__':
#     file_dir = r'H:\磁瓦\101训练集\seg_coco\zzz/'
#     save_dir = r'H:\磁瓦\101训练集\seg_coco\liewen_val/'
#     datanames = os.listdir(file_dir)
#     for dataname in datanames:
#         name = os.path.splitext(dataname)
#         if name[1] == '.json':
#             extract(path=file_dir + dataname, json_name=dataname, save_dir=save_dir)



#查看json文件中的label
file_dir = r'/media/hjh/workdir/0_Deep_Learning/磁瓦/测试数据集/语义分割（背面）/'
# write_file_name = r'ciwa.txt'
# write_file = open(write_file_name, "w")
json_files = []
i = 0
j = 0
datanames = os.listdir(file_dir)
for dataname in datanames:
    if os.path.splitext(dataname)[1] == '.json':
        with open(file_dir + dataname, 'r', encoding='utf-8') as jsonFile:
            data = json.load(jsonFile)
            try:
                shapes = data["shapes"]
                a = len(shapes)
                for i in range(a):
                    if shapes[i]['label'] == 'liewen':
                        write_file.write( '' + dataname + '\n')
                        print(dataname)

                    # print(shapes)
                    # write_file.write( '' + dataname + '\n')
            except:
                i = i+1
                print(dataname)
                # write_file.write( '' + dataname + '\n')
write_file.close()
print("无标注文件个数：", i) 
# print("label个数大于2的个数：", j)    

